package com.rongzhe.house.ui.fragment;

import android.Manifest;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.baidu.location.BDLocation;
import com.rongzhe.house.R;
import com.rongzhe.house.entity.SubwayInfo;
import com.rongzhe.house.entity.voo.Country;
import com.rongzhe.house.internet.DataListener;
import com.rongzhe.house.manager.HouseManager;
import com.rongzhe.house.manager.PositionManager;
import com.rongzhe.house.ui.activity.BaseActivity;
import com.rongzhe.house.ui.adapter.CountryAdapter;
import com.rongzhe.house.ui.adapter.SubwayInfoAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.OnItemClick;
import butterknife.Unbinder;

/**
 * Created by huang on 2017/12/23.
 */

public class PositionFilterFragment extends FilterFragment {
    @BindView(R.id.list_content)
    ListView listContent;
    Unbinder unbinder;
    private int mCurrentChoose = -1;
    private ListAdapter mListAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_position_filter, container, false);
        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishFragment();
            }
        });
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @OnClick(R.id.item_nearby)
    public void onNearbyClick() {
        listContent.setAdapter(null);
        ((BaseActivity) getActivity()).checkForPermission(
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION});
        showProgress(getString(R.string.loading));
    }

    @OnCheckedChanged({R.id.item_region, R.id.item_subway})
    public void onGroupChanged(CompoundButton button, boolean checked) {
        if (!checked) {
            return;
        }
        int checkedId = button.getId();
        switch (checkedId) {
            case R.id.item_region:
                loadCountry();
                break;
            case R.id.item_subway:
                loadSubway();
                break;
        }
        mCurrentChoose = checkedId;
    }

    @Override
    public void onStart() {
        super.onStart();
        PositionManager.getInstance().addBDLocationChangedListenr(mLocation);
    }

    @Override
    public void onStop() {
        super.onStop();
        PositionManager.getInstance().removeBDLocationChangedListenr(mLocation);
    }

    private PositionManager.BDLocationChangedListener mLocation
            = new PositionManager.BDLocationChangedListener() {
        @Override
        public void onBDLocationChanged() {
            bdLocation();
            finishFragment();
        }
    };

    private void bdLocation() {
        BDLocation bdLocation = PositionManager.getInstance().getBDLocation();
        if (bdLocation != null) {
            HouseManager.getInstance().setSearchNearby(true, bdLocation);
        }
    }

    public void onRequestPermissGranted(boolean granted, String permission) {
        if (granted) {
            BDLocation bdLocation = PositionManager.getInstance().getBDLocation();
            if (bdLocation != null) {
                HouseManager.getInstance().setSearchNearby(true, bdLocation);
            } else {
                PositionManager.getInstance().getLocationInfo(getContext());
            }
        } else {
            showToast(getString(R.string.need_location_permission));
        }
    }

    private void cleanAllFilter() {
        HouseManager.getInstance().setQueryRegion(null);
        HouseManager.getInstance().setSubway(null);
        HouseManager.getInstance().setSearchNearby(false, null);
    }

    @OnItemClick(R.id.list_content)
    public void onListItemClick(int position) {
        cleanAllFilter();
        switch (mCurrentChoose) {
            case R.id.item_region:
                HouseManager.getInstance().setSearchNearby(false, null);
                Country country = (Country) mListAdapter.getItem(position);
                HouseManager.getInstance().setQueryRegion(country.getCountyId().toString());
                break;
            case R.id.item_subway:
                HouseManager.getInstance().setSearchNearby(false, null);
                SubwayInfo subwayInfo = (SubwayInfo) mListAdapter.getItem(position);
                HouseManager.getInstance().setSubway(String.valueOf(subwayInfo.getId()));
                break;
            default:
                return;
        }
        finishFragment();
    }

    private void loadSubway() {
        PositionManager.getInstance().loadSubwayInfo(
                PositionManager.getInstance().getCurrentCityId(),
                new DataListener<List<SubwayInfo>>() {
                    @Override
                    public void onSuccess(List<SubwayInfo> data, String message) {
                        mListAdapter = new SubwayInfoAdapter(getContext());
                        ((SubwayInfoAdapter) mListAdapter).setData(data);
                        listContent.setAdapter(mListAdapter);
                    }

                    @Override
                    public void onFailed(Throwable error, String message) {
                        showToast(message);
                    }

                    @Override
                    public void onEnd() {

                    }
                });
    }

    private void loadCountry() {
        PositionManager.getInstance().loadCountryList(
                PositionManager.getInstance().getCurrentCityId(),
                new DataListener<List<Country>>() {
                    @Override
                    public void onSuccess(List<Country> data, String message) {
                        mListAdapter = new CountryAdapter(getContext());
                        ((CountryAdapter) mListAdapter).setData(data);
                        listContent.setAdapter(mListAdapter);
                    }

                    @Override
                    public void onFailed(Throwable error, String message) {
                        showToast(message);
                    }

                    @Override
                    public void onEnd() {

                    }
                });
    }

    @OnClick(R.id.item_clear)
    public void onViewClicked() {
        HouseManager.getInstance().setSearchNearby(false, null);
        HouseManager.getInstance().setQueryRegion(null);
        HouseManager.getInstance().setSubway(null);
        finishFragment();
    }
}
